package com.troila.rpc;

import com.google.common.collect.ImmutableList;
import com.troila.server.grpc.core.DataRpcServiceGrpc;
import com.troila.server.grpc.core.Message;
import com.troila.server.grpc.core.MessageChannelRequest;
import com.troila.server.grpc.core.Resource;
import com.troila.server.grpc.core.ResourcePageRequest;
import com.troila.server.grpc.core.ResourcePageResponse;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
@Slf4j
public class DataRpcService extends DataRpcServiceGrpc.DataRpcServiceImplBase {

    @Override
    public void findResourcePage(ResourcePageRequest request, StreamObserver<ResourcePageResponse> responseObserver) {
        // 模拟分页查询结果
        List<Resource> result = ImmutableList.of(
                Resource.newBuilder()
                        .setId("1")
                        .setName("张三")
                        .build(),
                Resource.newBuilder()
                        .setId("2")
                        .setName("李四")
                        .build());

        responseObserver.onNext(ResourcePageResponse.newBuilder()
                .setTotalPage(2)
                .addAllResource(result)
                .build());
        responseObserver.onCompleted();
    }

    @Override
    public void messageChannel(MessageChannelRequest request, StreamObserver<Message> responseObserver) {
        while (true) {
            responseObserver.onNext(Message.newBuilder()
                    .setId(UUID.randomUUID().toString())
                    .setContent("随机内容")
                    .build());
        }
    }
}
